from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from Apps.Project.filters.projectFilter import ProjectFilter
from Apps.Project.models import Project
from Apps.Project.ser.projectSer import ProjectSer, SelectProjectSer
from Apps.common.fileUp.file import file_save
from Apps.common.response import SuccessResponse


class ProjectView(ModelViewSet):
    queryset = Project.objects.all()
    serializer_class = ProjectSer
    filter_class = ProjectFilter

    # authentication_classes = []

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return SuccessResponse("新增成功")


class ProjectFile(APIView):
    """保存项目图标图像接口"""

    def post(self, request):
        path = file_save(request, "project")
        return SuccessResponse(path)


class SelectProjectView(APIView):
    """获取项目名称和ID，供下拉选择项目使用"""

    authentication_classes = []

    def get(self, request):
        serializer = SelectProjectSer(Project.objects.all(), many=True)
        return SuccessResponse(serializer.data)
